import {
	defineStore
} from 'pinia'
import {
	loginApi,
	phoneLoginApi,
	userInfoApi,
	logoutApi,
	getMyPage,
	getSubscribeState
} from '@/api/auth.js'

import {
	store
} from '@/stores/index.js'

const LOCAL_KEY_TOKEN = 'token'
const LOCAL_KEY_USER_INFO = 'userInfo'

export const useUserStore = defineStore('user', {
	state: () => {
		return {
			token: uni.getStorageSync(LOCAL_KEY_TOKEN),
			userInfo: uni.getStorageSync(LOCAL_KEY_USER_INFO),
			myPageInfo:{},
			wxPubInfo:{
				subscribeState:true,
				wxPubCodeUrl:''
			},
			wxPubState:{
				indexShow:true,
				tabMessageShow:true,
				personalMessageShow:true
			},
			applyType: '',
		}
	},
	getters: {
		isIndexWxPubShow: (state) => {
			return !!(state.token && !state.wxPubInfo.subscribeState && state.wxPubState.indexShow)
		},
		isTabWxPubShow: (state) => {
			return !!(state.token && !state.wxPubInfo.subscribeState && state.wxPubState.tabMessageShow)
		},
		isPersonalWxPubShow: (state) => {
			return !!(state.token && !state.wxPubInfo.subscribeState && state.wxPubState.personalMessageShow)
		},
		isLogin: (state) => {
			return !!(state.token && state.userInfo && state.userInfo.id)
		},
		// 是否策划人(商家)角色
		isMerchant: (state) => {
			return !!(state.userInfo && state.userInfo.businessInfo && state.userInfo.businessInfo.id)
		},
		// 是否经纪人角色
		isAgent: (state) => {
			return state.userInfo && state.userInfo.agentState === 2
		},
		// 商家注册地址信息
		locationInfo: (state) => {
			if (state.userInfo && state.userInfo.businessInfo) {
				return {
					provinceName: state.userInfo.businessInfo.provinceName,
					cityName: state.userInfo.businessInfo.cityName,
					address: state.userInfo.businessInfo.position,
				}
			} else {
				return {}
			}
		}
	},
	actions: {
	
		setPubShowState(data){
			// {key:'indexShow',value:false}
			this.wxPubState[data.key] = data.value
		},
		setToken(value) {
			this.token = value
			uni.setStorageSync(LOCAL_KEY_TOKEN, value)
		},
		async login(data) {
			const res = await loginApi(data)
			if (res.token) {
				this.token = res.token
				uni.setStorageSync(LOCAL_KEY_TOKEN, res.token)
			}
			return res
		},
		async phoneLogin(data) {
			const res = await phoneLoginApi(data)
			if (res.token) {
				this.token = res.token
				uni.setStorageSync(LOCAL_KEY_TOKEN, res.token)
			}
			return res
		},
		async getUserInfo() {
			const res = await userInfoApi()
			if (res) {
				this.userInfo = res
				uni.setStorageSync(LOCAL_KEY_USER_INFO, res)
			}
		},
		async getWxRead() {
			const res = await getSubscribeState()
			// console.log(res,'ddd')
			if(res){
				this.wxPubInfo = res
			}
		},
		setUserInfo(data) {
			this.userInfo = data
			uni.setStorageSync(LOCAL_KEY_USER_INFO, data)
		},
		// // 我的页面数据-个人中心提现等数据
		// async getMyPage () {
		// 	const res = await getMyPage()
		// 	if (res) {
		// 		this.myPageInfo = res
		// 		// uni.setStorageSync(LOCAL_KEY_USER_INFO, res)
		// 	}
		// },
		async logout() {
			const res = await logoutApi()
			// uni.setStorageSync(LOCAL_KEY_USER_INFO, null)
			this.setUserInfo(null)
			this.setToken(null)
			uni.$emit('logout')
			uni.reLaunch({
				url: '/pagesA/login/login'
			})
		},
		setApplyType(type){
			this.applyType = type
		},
	}
})

export const useUserStoreWithOut = () => {
	return useUserStore(store)
}